package com.example.resturant.controller;

import com.example.resturant.dto.AddressDTO;
import com.example.resturant.dto.LoginRequestDTO;
import com.example.resturant.dto.RegisterRequestDTO;
import com.example.resturant.service.UserService;
import com.example.resturant.vo.AddressVO;
import com.example.resturant.vo.ResultVO;
import com.example.resturant.vo.UserVO;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("/user")
@Controller
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/personal")
    public String personal(Model model, HttpSession httpSession) {
        Boolean logined = (Boolean) httpSession.getAttribute("logined");
        if (BooleanUtils.isNotTrue(logined)) {
            return "redirect:/index";
        }
        UserVO userVO = (UserVO) httpSession.getAttribute("user");
        if (userVO == null) {
            return "redirect:/index";
        }
        List<AddressVO> addresses = userService.listAddresses(userVO.getId());
        model.addAttribute("addresses", addresses);
        return "personal";
    }

    @PostMapping("/login")
    @ResponseBody
    public ResultVO login(@RequestBody LoginRequestDTO loginRequest, HttpSession httpSession) {
        ResultVO resultVO = new ResultVO();
        try {
            UserVO userVO = userService.login(loginRequest);
            httpSession.setAttribute("user", userVO);
            httpSession.setAttribute("logined", true);
        } catch (Exception e) {
            resultVO.setCode(-1);
            resultVO.setMessage(e.getMessage());
        }
        return resultVO;
    }

    @PostMapping("/register")
    @ResponseBody
    public ResultVO register(@RequestBody RegisterRequestDTO registerRequest, HttpSession httpSession) {
        ResultVO resultVO = new ResultVO();
        try {
            UserVO userVO = userService.register(registerRequest);
            httpSession.setAttribute("user", userVO);
            httpSession.setAttribute("logined", true);
        } catch (Exception e) {
            resultVO.setCode(-1);
            resultVO.setMessage(e.getMessage());
        }
        return resultVO;
    }

    @PostMapping("/logout")
    @ResponseBody
    public void logout(HttpSession httpSession) {
        httpSession.invalidate();
    }

    @PostMapping("/address")
    public String addAddress(@ModelAttribute AddressDTO address, HttpSession httpSession) {
        Boolean logined = (Boolean) httpSession.getAttribute("logined");
        if (BooleanUtils.isNotTrue(logined)) {
            return "redirect:/index";
        }
        UserVO userVO = (UserVO) httpSession.getAttribute("user");
        if (userVO == null) {
            return "redirect:/index";
        }
        userService.saveAddress(userVO.getId(), address);
        return "redirect:/user/personal";
    }

    @GetMapping("/delAddress")
    public String delAddress(@RequestParam Integer addressId, HttpSession httpSession) {
        Boolean logined = (Boolean) httpSession.getAttribute("logined");
        if (BooleanUtils.isNotTrue(logined)) {
            return "redirect:/index";
        }
        UserVO userVO = (UserVO) httpSession.getAttribute("user");
        if (userVO == null) {
            return "redirect:/index";
        }
        userService.delAddress(addressId);
        return "redirect:/user/personal";
    }
}
